package com.qf.service.impl;

import com.qf.entity.Classroom;
import com.qf.entity.Student;
import com.qf.entity.StudentVO;
import com.qf.entity.Subject;
import com.qf.mapper.ClassroomMapper;
import com.qf.mapper.StudentMapper;
import com.qf.mapper.SubjectMapper;
import com.qf.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * --- ONE PIECE ---
 *
 * @author yangjiangcheng
 * @date 2025/8/15
 * @desc
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ClassroomMapper classroomMapper;

    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public Student findStuById(int id) {
        return studentMapper.findStuById(id);
    }

    @Override
    public List<Student> studentList() {
        return studentMapper.studentList();
    }

    @Override
    public List<Student> listByMap(Map<Integer, String> map) {
        return studentMapper.listByMap(map);
    }

    @Override
    public int studentAdd(Student student) {
        return studentMapper.studentAdd(student);
    }

    @Override
    public int studentUpdate(Student student) {
        return studentMapper.studentUpdate(student);
    }

    @Override
    public int studentDelete(int id) {
        return studentMapper.studentDelete(id);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        studentMapper.deleteBatch(ids);
    }

    @Override
    public List<StudentVO> findAllStudentAndClassroom() {
        return studentMapper.findAllStudentAndClassroom();
    }

    @Override
    public StudentVO findStudentAndAllSubjectByStuid(int sid) {
        return studentMapper.findStudentAndAllSubjectByStuid(sid);
    }

    @Override
    public StudentVO findStudentAndAllSubjectAndTeacherByStuid(int sid) {
        return studentMapper.findStudentAndAllSubjectAndTeacherByStuid(sid);
    }

    @Override
    public List<StudentVO> findAllStudentAndClassroomV2() {
        // 创建最终的结果集合,存储学生信息及教室信息
        List<StudentVO> list = new ArrayList<>();

        // 查询所有学生
        List<Student> studentList = studentMapper.studentList();
        // 遍历得到所有学生
        studentList.forEach(student -> {
            // 再根据学生对象中的教室id查询对应的教室
            Classroom classroom = classroomMapper.findClassroomById(student.getCid());
            // 创建最终封装数据的对象
            StudentVO studentVO = new StudentVO();
            // BeanUtils是spring框架提供的工具类,copyProperties方法用与拷贝对象属性
            // 此处是将student对象中的属性赋值到studentVO中
            BeanUtils.copyProperties(student, studentVO);
            // studentVO中存储教室信息
            studentVO.setClassroom(classroom);
            // 将每一个StudentVO添加到List集合
            list.add(studentVO);
        });
        return list;
    }

    @Override
    public StudentVO findStudentAndAllSubjectByStuidV2(int sid) {
        // 先根据学生id查出1个学生
        Student student = studentMapper.findStuById(sid);

        // 创建集合准备存储每个学科对象
        List<Subject> subjectList = new ArrayList<>();
        // 再根据学生id查出关联表中所关联的所有学科id
        List<Integer> subIdList = studentMapper.findAllSubjectId(sid);
        // 遍历所有id
        subIdList.forEach(subId -> {
            // 再根据学科id获得每一个学科对象
            Subject subject = subjectMapper.findSubjectById(subId);
            // 存储学科对象
            subjectList.add(subject);
        });

        // 封装数据到VO
        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(student, studentVO);
        studentVO.setSubjectListV2(subjectList);
        return studentVO;
    }
}
